package org.phantom.securityframework.local;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.phantom.securityframework.api.Account;
import org.phantom.securityframework.api.Permission;
import org.phantom.securityframework.api.PermissionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.phantom.securityframework.api.domain.Group;
import org.phantom.securityframework.api.domain.Module;
import org.phantom.securityframework.api.domain.Role;
import org.phantom.securityframework.api.util.AuthUtil;
import org.phantom.securityframework.auth.realm.PermissionRealm;

@Service
@Transactional
public class PermissionManagerLocalImpl implements PermissionManager,PermissionRealm{


	@Resource
	private SqlSession session;
	

	@Override
	public Permission get(int id) {
		return (Permission) session.selectOne("org.phantom.securityframework.PermissionMapper.get",id);
	}
	
	
	@Override
	public void relAccountPermission(Account a, Permission[] permissions) {
		org.phantom.securityframework.api.domain.Account account = (org.phantom.securityframework.api.domain.Account) a;

		List<Integer> list = new ArrayList<Integer>();
		for (Permission p : permissions) {
			list.add(p.getId());
		}
		this.relAccountPermission(account.getId(), list.toArray(new Serializable[]{}));
	}

	@Override
	public void relAccountPermission(Account a, Serializable[] permissionIds) {
		org.phantom.securityframework.api.domain.Account account = (org.phantom.securityframework.api.domain.Account) a;
		this.relAccountPermission(account.getId(), permissionIds);
		
	}

	@Override
	public void relAccountPermission(Serializable accountId, Serializable[] permissionIds) {
		if(accountId==null){
			throw new RuntimeException("accountId can not be null!");
		}
		Map<String,String> params = new HashMap<String,String>();
		for (Serializable id : permissionIds) {
			params.put("accountId", (String) accountId);
			params.put("permId",(String)id);
			session.insert("org.phantom.securityframework.PermissionMapper.relAccountPermission",params);
			params.clear();
		}
	}

	@Override
	public void unrelAccountPermission(Account a, Permission[] permissions) {
		org.phantom.securityframework.api.domain.Account account = (org.phantom.securityframework.api.domain.Account) a;
		List<Integer> list = new ArrayList<Integer>();
		for (Permission p : permissions) {
			list.add(p.getId());
		}
		this.unrelAccountPermission(account.getId(), list.toArray(new Serializable[]{}));		
	}

	@Override
	public void unrelAccountPermission(Account a,
			Serializable[] permissionIds) {
		org.phantom.securityframework.api.domain.Account account = (org.phantom.securityframework.api.domain.Account) a;
		this.unrelAccountPermission(account.getId(), permissionIds);
		
	}

	@Override
	public void unrelAccountPermission(Serializable accountId,
			Serializable[] permissionIds) {

		if(accountId==null){
			throw new RuntimeException("accountId can not be null!");
		}
		Map<String,String> params = new HashMap<String,String>();
		for (Serializable id : permissionIds) {
			params.put("accountId",(String) accountId);
			params.put("permId",(String)id);
			session.delete("org.phantom.securityframework.PermissionMapper.unrelAccountPermission",params);
			params.clear();
		}		
	}

	@Override
	public void relGroupPermission(Group group, Permission[] permissions) {
		List<Integer> list = new ArrayList<Integer>();
		for (Permission p : permissions) {
			list.add(p.getId());
		}
		this.relGroupPermission(group.getId(), list.toArray(new Serializable[]{}));		
	}

	@Override
	public void relGroupPermission(Group group, Serializable[] permissions) {
		this.relGroupPermission(group.getId(), permissions);		
	}

	@Override
	public void relGroupPermission(Serializable groupId,
			Serializable[] permissionIds) {
		if(groupId==null){
			throw new RuntimeException("groupId can not be null!");
		}
		Map<String,String> params = new HashMap<String,String>();
		for (Serializable id : permissionIds) {
			params.put("groupId", (String) groupId);
			params.put("permId",(String)id);
			session.insert("org.phantom.securityframework.PermissionMapper.relGroupPermission",params);
			params.clear();
		}				
	}

	@Override
	public void unrelGroupPermission(Group group, Permission[] permissions) {
		List<Integer> list = new ArrayList<Integer>();
		for (Permission p : permissions) {
			list.add(p.getId());
		}
		this.unrelGroupPermission(group.getId(), list.toArray(new Serializable[]{}));			
	}

	@Override
	public void unrelGroupPermission(Group group, Serializable[] permissions) {

		this.unrelGroupPermission(group.getId(), permissions);				
	}

	@Override
	public void unrelGroupPermission(Serializable groupId,
			Serializable[] permissionIds) {
		if(groupId==null){
			throw new RuntimeException("groupId can not be null!");
		}
		Map<String,Integer> params = new HashMap<String,Integer>();
		for (Serializable id : permissionIds) {
			params.put("groupId", (Integer) groupId);
			params.put("permId",(Integer)id);
			session.delete("org.phantom.securityframework.PermissionMapper.unrelGroupPermission",params);
			params.clear();
		}					
	}

	@Override
	public void relRolePermission(Role role, Permission[] permissions) {
		List<Integer> list = new ArrayList<Integer>();
		for (Permission p : permissions) {
			list.add(p.getId());
		}
		this.relRolePermission(role.getId(), list.toArray(new Serializable[]{}));	
	}

	@Override
	public void relRolePermission(Role role, Serializable[] permissionIds) {
		this.relRolePermission(role.getId(), permissionIds);	
	}

	@Override
	public void relRolePermission(Serializable roleId,
			Serializable[] permissionIds) {

		if(roleId==null){
			throw new RuntimeException("roleId can not be null!");
		}
		Map<String,String> params = new HashMap<String,String>();
		for (Serializable id : permissionIds) {
			params.put("roleId", (String) roleId);
			params.put("permId",(String)id);
			session.insert("org.phantom.securityframework.PermissionMapper.relRolePermission",params);
			params.clear();
		}										
	}

	@Override
	public void unrelRolePermission(Role role, Permission[] permissions) {
		List<Integer> list = new ArrayList<Integer>();
		for (Permission p : permissions) {
			list.add(p.getId());
		}
		this.unrelRolePermission(role.getId(), list.toArray(new Serializable[]{}));		
	}

	@Override
	public void unrelRolePermission(Role role, Serializable[] permissionIds) {
		this.unrelRolePermission(role.getId(), permissionIds);	
		
	}

	@Override
	public void unrelRolePermission(Serializable roleId,
			Serializable[] permissionIds) {

		if(roleId==null){
			throw new RuntimeException("roleId can not be null!");
		}
		Map<String,String> params = new HashMap<String,String>();
		for (Serializable id : permissionIds) {
			params.put("roleId", (String) roleId);
			params.put("permId",(String)id);
			session.delete("org.phantom.securityframework.PermissionMapper.unrelRolePermission",params);
			params.clear();
		}			
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<org.phantom.securityframework.api.Permission> list(String subsystem) {
		Map<String,String> params = new HashMap<String, String>();
		params.put("platform", AuthUtil.getPlatformId());
		params.put("subsystem", subsystem);
		return session.selectList("org.phantom.securityframework.PermissionMapper.list",params);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Permission> findSubsystemPermission(String platformId,
			String subsystemId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("platform", platformId);
		params.put("subsystem", subsystemId);
		return session.selectList("org.phantom.securityframework.PermissionMapper.list",params);
	}

	@Override
	public Permission save(Permission permission) {
		if(permission==null)return null;
				
		session.insert("org.phantom.securityframework.PermissionMapper.insertPermission",permission);
		return permission;
	}

	@Override
	public Permission save(Permission permission, int moduleId) throws Exception {
		if(moduleId<=0){
			throw new Exception("module id cannot be null!");
		}
		permission = this.save(permission);
		Map<String, Integer> params = new HashMap<String, Integer>();
		params.put("permId",permission.getId());
		params.put("moduleId", moduleId);
		session.insert("org.phantom.securityframework.PermissionMapper.relModulePermission",params);
		return permission;
	}

	@Override
	public Permission save(Permission permission, Module module) throws Exception {
		if(module.getId()<=0){
			throw new Exception("module id cannot be null!");
		}
		permission = this.save(permission);
		Map<String, Integer> params = new HashMap<String, Integer>();
		params.put("permId",permission.getId());
		params.put("moduleId",module.getId());
		session.insert("org.phantom.securityframework.PermissionMapper.relModulePermission",params);
		return permission;
	}


	
	//**********************************************************************************
	//							权限分配这部分后期实现
	//**********************************************************************************


	@Override
	public List<Permission> list4mgt(int accountId) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void mgtAccountPermission(int accountId, int[] permissionIds) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void unmgtAccountPermission(int accountId, int[] permissionIds) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public Permission get(String permName) {
		// TODO Auto-generated method stub
		return null;
	}

}
